package com.dferreira.languagesteach.data_adapters.generic;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dferreira.languagesteach.LanguagesTeachApplication;
import com.dferreira.languagesteach.R;
import com.dferreira.languagesteach.activities.generic.IFragmentStack;
import com.dferreira.languagesteach.constants.Constants;
import com.dferreira.languagesteach.custom_items.UITableViewCell;
import com.dferreira.languagesteach.utils.DeviceStretcher;


public abstract class NavigationAdapter extends ArrayAdapter<UITableViewCell> implements View.OnClickListener {
    private final String TAG = "NavigationAdapter";

    private boolean dataSetBuilt;
    private final DeviceStretcher deviceStretcher;
    private final Context context;
    private final String packageInstalledFolder;
    private IFragmentStack fragmentStack;

    /**
     *
     * @param context
     * @param packageInstalledFolder
     */
    protected NavigationAdapter(Context context, String packageInstalledFolder) {
        super(context, 0);
        this.dataSetBuilt = false;
        this.deviceStretcher = new DeviceStretcher(context);
        this.packageInstalledFolder = packageInstalledFolder;
        this.context = context;
    }

    /**
     * Indicate than the dataSet was well constructed
     */
    protected void dataSetWellConstructed() {
        dataSetBuilt = true;
    }

    /**
     * @return if the dataSet was built or not
     */
    public boolean isDataSetBuilt() {
        return dataSetBuilt;
    }

    /**
     * @return path to the the folder where the package was installed
     */
    protected String getPackageInstalledFolder() {
        return packageInstalledFolder;
    }

    /**
     * @return false   -> don't must keep restoring the last session
     * true    -> Needs to keep restoring the the last session
     */
    protected boolean doSessionRestore() {
        String controllerClassName;
        String lastSessionClassName;

        controllerClassName = context.getClass().getName();
        lastSessionClassName = LanguagesTeachApplication.getApplicationState().getCurrentClassController();
        return !((!isDataSetBuilt()) || (controllerClassName.equals(lastSessionClassName)));
    }

    /**
     * Push any kind of activity the needs to be pushed to the front of the screen
     *
     * @param className Name from the class from the activity to push
     */
    protected void pushExerciseActivity(String className) {
        Class<?> clazz;

        Intent intent;

        try {
            clazz = Class.forName(className);
            intent = new Intent(this.context, clazz);
            intent.putExtra(Constants.PACKAGE_FOLDER_KEY, getPackageInstalledFolder());
            context.startActivity(intent);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, TAG, e);
        }
    }

    /**
     * @return
     */
    protected IFragmentStack getFragmentStack() {
        return fragmentStack;
    }

    /**
     * @param fragmentStack
     */
    protected void setFragmentStack(IFragmentStack fragmentStack) {
        this.fragmentStack = fragmentStack;
    }

    /**
     * @param position    The position of the item within the adapter's data set of the item whose view we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view is non-null and
     *                    of an appropriate type before using. If it is not possible to convert this view to display
     *                    the correct data, this method can create a new view. Heterogeneous lists can specify their
     *                    number of view types, so that this View is always of the right type (see getViewTypeCount()
     *                    and getItemViewType(int)).
     * @param parent      The parent that this view will eventually be attached to
     * @return the view to be rendered by system inflate a costumed view that
     * provided a image/label pair set the label from the row set the image from
     * the row set the tag which identify the row set the listener from the row
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Drawable flagImg;

        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.table_view_cell_style_default,
                parent, false);

        if (row == null) {
            return null;
        }
        TextView textLabel = (TextView) row.findViewById(R.id.textLabel);
        ProgressBar progressBar = (ProgressBar) row.findViewById(R.id.progressBar);

        flagImg = getItem(position).getImageView();
        textLabel.setText(getItem(position).getTextLabel());
        textLabel.setCompoundDrawablesWithIntrinsicBounds(deviceStretcher.zoomDrawable(flagImg), null, null, null);
        textLabel.setVisibility(android.view.View.VISIBLE);

        //If has progress in that item show the progress bar to the user
        if (getItem(position).getProgress() != null) {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(getItem(position).getProgress());
        }

        row.setTag(getItem(position).getTag());

        row.setOnClickListener(this);
        return row;
    }
}
